package com.fzrskj.cloud.free.user.service;

import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.tool.CDateTimeTool;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.user.bean.UserMessage;
import com.fzrskj.cloud.free.user.utils.UserMessageUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author wangxinhao 
 * @version 2016-9-30
 */
public class UserMessageService extends CObject {
	/**
	 * 查询userMessage信息
	 * @param request
	 * @param response
	 * @param p={"msgType":n}
	 * @param sid
	 * @return
	 */
	public String queryUserMessage(HttpServletRequest request, HttpServletResponse response, String p, String sid){
		int uid = CloudContextUtils.getSession().getUID();
		int msg = CJsonTool.getIntValue(p, "msgType", -1);
		int page = CJsonTool.getIntValue(p, "page", 1);
		int pagesize = CJsonTool.getIntValue(p, "pagesize", CConsts.PAGE_SIZE);

		UserMessageUtils utils = new UserMessageUtils();
		int rows = utils.queryUserMessage(uid, msg);

		String code = utils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			utils.dispose();
			return XmlCodeParser.formatResultTips(false, code);
		}
		if (rows < (page - 1) * pagesize) {
			utils.dispose();
			return XmlCodeParser.formatResultTips(false, code);
		}
		int pages = Math.round((rows + pagesize - 1) / pagesize);
		List<UserMessage> list = utils.queryUserMessageList(uid, msg, page, pagesize);
		code = utils.getErrCode();
		utils.dispose();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatCustomTips(false, code);
		}
		String msgTime = null;
		String data = "[";
		String row;
		for (UserMessage userMessage : list) {
			msgTime = CDateTimeTool.dateToDateTimeString(userMessage.getMsgTime());
			row = CJsonTool.addNode(CJsonTool.beanToJson(userMessage), "msgTime", msgTime);
			data  = data + row + ",";
		}
		data = CStrTool.deleteCharAt(data, data.length() - 1);
		data = data + "]";
		return XmlCodeParser.formatResultPageData(code, page, pages, pagesize, rows, data);
	}
	/**
	 * 添加UserMessage信息
	 * @param request
	 * @param response
	 * @param p={"content":"公告内容"，"msgTime":"公告时间","msgType":"公告类型"}
	 * @param sid
	 * @return
	 */
	public String addUseMessage(HttpServletRequest request, HttpServletResponse response, String p, String sid){
		String content = CJsonTool.getStrValue(p, "content", null);
		String msgTime = CJsonTool.getStrValue(p, "msgTime", null);
		int msgType = CJsonTool.getIntValue(p, "msgType", -1);

		Date time = CDateTimeTool.strToDateTime(msgTime);

		if (CStrTool.isBlankStr("content") || CStrTool.isBlankStr("msgTime") || CStrTool.isBlankStr("msgType")) {
			this.log(p + "invalid new user_message parameter");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		UserMessageUtils utils = new UserMessageUtils();
		int ret = utils.addUserMessage(content, time, msgType);
		boolean success = (ret > 0);
		String code = utils.getErrCode();
		utils.dispose();
		return XmlCodeParser.formatResultTips(success, code);
	}
	/**
	 * 根据ID删除单个消息
	 * @param request
	 * @param response
	 * @param p{"id":"ID"}
	 * @param sid
	 * @return
	 */
	public String delUserMessage(HttpServletRequest request, HttpServletResponse response, String p, String sid){
		int id = CJsonTool.getIntValue(p, "id", -1);
		UserMessageUtils utils = new UserMessageUtils();
		if ((utils.delUserMessage(id)) > 0) {
			String code = utils.getErrCode();
			utils.dispose();
			return XmlCodeParser.formatResultTips(true, code);
		} else {
			String code = utils.getErrCode();
			utils.dispose();
			return XmlCodeParser.formatResultTips(false, code);
		}
	}
	/**
	 * 批量删除消息
	 * @param request
	 * @param response
	 * @param p{"utils":[{"id":"id","userName":"userName"}]}
	 * @param sid
	 * @return
	 */
	public String delUserMessageList(HttpServletRequest request, HttpServletResponse response, String p, String sid){
		String userName = CJsonTool.getStrValue(p, "userName", "");
		String listJson = CJsonTool.getStrValue(p, "userMessage", "");
		List<UserMessage> messagesIds = CJsonTool.jsonToListBean(listJson, UserMessage.class);
		UserMessageUtils utils = new UserMessageUtils();
		if ((utils.delUserMessageList(messagesIds, userName)) > 0) {
			String code = utils.getErrCode();
			utils.dispose();
			return XmlCodeParser.formatResultTips(true, code);
		} else {
			String code = utils.getErrCode();
			utils.dispose();
			return XmlCodeParser.formatResultTips(false, code);
		}
	}
	/**
	 * 标记已读消息
	 * @param request
	 * @param response
	 * @param p{"state":"1","userName":"userName"}
	 * @param sid
	 * @return
	 */
	public String updateState(HttpServletRequest request, HttpServletResponse response, String p, String sid){
		int state = CJsonTool.getIntValue(p, "state", -1);
		String userName = CJsonTool.getStrValue(p, "userName", "");
		UserMessageUtils utils = new UserMessageUtils();
		if (utils.upState(state, userName) > 1) {
			String code = utils.getErrCode();
			utils.dispose();
			return XmlCodeParser.formatResultTips(true, code);
		} else {
			String code = utils.getErrCode();
			utils.dispose();
			return XmlCodeParser.formatResultTips(false, code);
		}

	}
	/**
	 * 批量标记已读消息
	 * @param request
	 * @param response
	 * @param p{"utils":[{"id":"id","userName":"userName"，"state":"1"}]}
	 * @param sid
	 * @return
	 */
	public String updateStateList(HttpServletRequest request, HttpServletResponse response, String p, String sid){
		int state = CJsonTool.getIntValue(p, "state", -1);
		String listJson = CJsonTool.getStrValue(p, "userMessage", "");
		List<UserMessage> messageIds = CJsonTool.jsonToListBean(listJson, UserMessage.class);
		UserMessageUtils utils = new UserMessageUtils();
		if ((utils.upStateList(messageIds, state)) > 1) {
			String code = utils.getErrCode();
			utils.dispose();
			return XmlCodeParser.formatResultTips(true, code);
		} else {
			String code = utils.getErrCode();
			utils.dispose();
			return XmlCodeParser.formatResultTips(false, code);
		}
	}
}
